const { ccclass, property } = cc._decorator;

@ccclass
export default class CupHolderChoseCtrl extends cc.Component {
    @property(cc.Node)
    btn_sure: cc.Node = null;
    @property(cc.Node)
    btn_close: cc.Node = null;
    @property(cc.Node)
    btn_reset: cc.Node = null;
    @property(cc.Node)
    btn_start: cc.Node = null;
    @property(cc.Node)
    btn_over: cc.Node = null;
    @property(cc.Node)
    gameTouchNode: cc.Node = null;
    @property(cc.Node)
    mainNode: cc.Node = null;
    @property(cc.Prefab)
    fixedElementPrefab: cc.Prefab = null;

    private startTime: number = null;
    private startNode: cc.Node = null;
    private isClickStart: boolean = false;

    onLoad() {
        this.createMap();
        this.addButtonEvent();
        this.addEvent();
    };

    start() { };

    addEvent() {
        this.gameTouchNode.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.gameTouchNode.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.gameTouchNode.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.gameTouchNode.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
    };

    addButtonEvent() {
        this.btn_close.on(cc.Node.EventType.TOUCH_END, this.clickClose, this);
        this.btn_start.on(cc.Node.EventType.TOUCH_END, this.clickStart, this);
        this.btn_over.on(cc.Node.EventType.TOUCH_END, this.clickOver, this);
        this.btn_sure.on(cc.Node.EventType.TOUCH_END, this.saveData, this);
        this.btn_reset.on(cc.Node.EventType.TOUCH_END, this.clickReset, this);
    };

    clickClose() {
        this.node.active = false;
        this.clickReset();
    };

    clickStart() {
        this.isClickStart = true;
        p.creatNewPath();
    };

    clickOver() {
        this.isClickStart = false;
        p.setFinalStr();
    };

    saveData() {
        if (this.isClickStart) {
            s.showManageViewToast("请点击开始");
        } else {
            var e = p.getInitStrArr(),
                t = p.getInitPosData();
            l.log("保存茶杯的数据=====", t);
            for (var a = 0; a < t.length; a++) {
                var o = t[a][0],
                    i = e[a];
                Object.assign(o, {
                    isCanPlace: true,
                    init_str: i
                });
                this.mainNode.getComponent(d).clickBlockItem(o);
            }
            this.clickClose();
        }
    };

    clickReset() {
        this.gameTouchNode.children.forEach( (e) => {
            e.getComponent(u).setChoseSp(false);
        });
        p.clearPath();
        this.isClickStart = false;
    };

    createMap() {
        this.gameTouchNode.removeAllChildren();
        for (var e = 0; e < 9; e++) {
            for (var t = 0; t < 11; t++) {
                var a = cc.instantiate(this.fixedElementPrefab);
                a.x = 48 + e * a.width;
                a.y = -48 - t * a.height;
                a.name = "bottom_" + e + "_" + t;
                this.gameTouchNode.addChild(a);
                a.getComponent(u).initData({
                    row: e,
                    col: t
                });
            }
        }
        p.clearPath();
        this.isClickStart = false;
    };

    onTouchStart(e) {
        if (this.isClickStart) {
            var t = this.getTouchSquare(e);
            if (t) {
                var a = t.getComponent(u);
                l.log("onTouchStart ===选择需要布置的格子", a.row, a.col);
                this.startNode = t;
                this.startTime = Date.now();
            }
        } else s.showManageViewToast("请点击开始");
    };

    onTouchMove(e) {
        this.isClickStart ? this.setChoseSquare(e) : s.showManageViewToast("请点击开始");
    };

    onTouchEnd(e) {
        if (this.isClickStart) {
            this.checkClick(e);
            this.scheduleOnce(function () {
                s.showManageViewToast("布置完成记得点结束");
            }, 0.5);
        } else s.showManageViewToast("请点击开始");
    };

    onTouchCancel() {
        l.log("触摸取消===========");
    };

    getTouchSquare(e) {
        for (var t = this.gameTouchNode.convertToNodeSpaceAR(e.getLocation()), a = this.gameTouchNode.children, o = null, i = 0; i < a.length; i++) {
            var r = a[i];
            if (r.getBoundingBox().contains(t)) {
                o = r;
                break;
            }
        }
        return o;
    };

    setChoseSquare(e) {
        var t = this.getTouchSquare(e);
        if (t) {
            t.getComponent(u).setChoseSp(true);
        }
    };

    checkClick(e) {
        var t = this.getTouchSquare(e);
        if (t && this.startNode) {
            if (t.name == this.startNode.name) {
                if (Date.now() - this.startTime >= 300) {
                    this.longClickSquareItem(t);
                } else {
                    this.setChoseSquare(e);
                }
                this.startNode = null;
                this.startTime = null;
            }
        } else {
            l.log("触摸结束没有获取到触摸的节点");
        }
    };

    longClickSquareItem(e) {
        e.getComponent(u).setChoseSp(false);
    };
}
